package com.briup.cms.web.controller;

import com.briup.cms.bean.BaseConfig;
import com.briup.cms.log.LoggingAccess;
import com.briup.cms.service.IBaseConfigService;
import com.briup.cms.utils.Message;
import com.briup.cms.utils.MessageUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 配置信息的控制器类
 */
@Api(tags = "系统配置相关接口")
@RestController
@RequestMapping("/auth/config")
public class BaseConfigController {
	
	@Autowired
	private IBaseConfigService baseConfigService;

	@ApiOperation(value = "查询所有配置信息",notes = "分页查询，需要传入当前页和每个大小")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "当前页", required = true, paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, paramType = "query"),
	})
	@GetMapping("/page/query")
	public Message pageQuery(int pageNum, int pageSize) {
		PageInfo<BaseConfig> data = baseConfigService.findAllWithPage(pageNum,pageSize);
		return MessageUtil.success(data);
	}

	@LoggingAccess
	@ApiOperation(value = "更新配置信息状态",notes = "该接口不会更新配置状态")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true),
		@ApiImplicitParam(name = "status",value = "状态，0-禁用，1-启用",paramType = "query",required=true)
	})
	@PutMapping("/update/status")
	public Message changeConfigStatus(int id,int status){
		baseConfigService.changeConfigStatus(id,status);
		return MessageUtil.success("配置信息状态修改成功");
	}

	@ApiOperation(value = "通过id删除配置信息")
	@ApiImplicitParams(
		@ApiImplicitParam(name = "id",value = "主键",paramType = "query",required=true)
	)
	@DeleteMapping("/deleteById")
	public Message deleteById(int id){
		baseConfigService.deleteById(id);
		return MessageUtil.success("删除成功");
	}

	@ApiOperation(value = "保存配置信息",notes = "不需要传configId")
    @PostMapping("/save")
    public Message saveConfig(BaseConfig config){
		baseConfigService.saveConfig(config);
        return MessageUtil.success("保存成功");
    }

	@ApiOperation(value = "更新配置",notes = "该接口不会更新配置状态")
	@PutMapping("/update")
	public Message updateConfig(BaseConfig config){
		baseConfigService.updateConfig(config);
		return MessageUtil.success("更新成功");
	}

	@ApiOperation(value = "查找当前启用的系统配置",notes = "当前最多只会有一个配置启用")
	@GetMapping("/current/config/yes")
	public Message currentConfigYes(){
		BaseConfig baseConfig = baseConfigService.currentConfigYes();
		return MessageUtil.success(baseConfig);
	}

}
